package com.woke.addressactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.alibaba.fastjson.JSONArray;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myfenrunadapter;
import com.woke.data.Datas_fenrun;
import com.woke.diyview.XListView;
import com.woke.diyview.XListView.IXListViewListener;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Userinfo_fenrunActivity extends Activity implements OnItemClickListener, IXListViewListener{

	private XListView mListView;
	private MyApp application;
	private ArrayList<Datas_fenrun> alllist = new ArrayList<Datas_fenrun>();
	private Myfenrunadapter adapter;
	private int pageno = 1;
	private LayoutInflater inflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo_fenrun);
		inflater = getLayoutInflater();
		application = (MyApp) getApplication();
		intview();
		
	}

	private void intview() {
		mListView = (XListView) findViewById(R.id.userinfo_fenrun_listview);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(this);
//		adapter = new Myadapter();
		adapter = new Myfenrunadapter(inflater, alllist);
		mListView.setAdapter(adapter);
		findViewById(R.id.userinfo_fenrun_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		getdata();
	}
	
	private void getdata() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "Scancode");
		params.put("a", "getProfit");
		params.put("userId", application.getDatas_load().getId());
		params.put("page", pageno);
//		http://app.88china.com:8384/index.php?g=api&m=Scancode&a=getProfit&userId=1147&page=1
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
//				Log.e(application.getDatas_load().getId(), ""+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jsonObject2 = jsonObject.getJSONObject("data");
						String string = jsonObject2.getString("list");
						ArrayList<Datas_fenrun> datalist = (ArrayList<Datas_fenrun>) JSONArray.parseArray(string, Datas_fenrun.class);
						alllist.addAll(datalist);
						adapter.notifyDataSetChanged();
						pageno = pageno+1;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageno = 1;
		getdata();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		getdata();
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		// TODO Auto-generated method stub
		
	}
	
}
